文件上传
依赖导入
<dependency>
<groupId>com.qcloud</groupId>
<artifactId>cos_api</artifactId>
<version>5.6.155</version>
</dependency>
注入COSClient实例
CosConfig.java
/**
* @author 追梦路上的孩子
* @version 1.0
* @date 2024/1/19 9:27
*/
@Configuration
@EnableConfigurationProperties(CosConfigProperties.class)
public class CosConfig {
@Autowired
private CosConfigProperties cosConfigProperties;
@Bean
public COSClient cosClient() {
BasicCOSCredentials cred = new BasicCOSCredentials(cosConfigProperties.getSecretId(), cosConfigProperties.getSecretKey());
Region region = new Region(cosConfigProperties.getRegion());
ClientConfig clientConfig = new ClientConfig(region);
return new COSClient(cred, clientConfig);
}
}
CosConfigProperties.java
/**
* @author 追梦路上的孩子
* @version 1.0
* @date 2024/1/19 9:27
*/
@Data
@ConfigurationProperties("tencent.cos")
public class CosConfigProperties {
private String secretId;
private String secretKey;
private String region;
private String bucketName;
private String baseUrl;
}
配置密钥和属性
application.yml
# 腾讯云cos配置
tencent:
cos:
secret-id: AKIDymcr03vqhNwPNDznP6iDWdnwxxxxxxxxx (填写自己的密钥id)
secret-key: pZDxeSG4wndN2cz83GNY5lQTgrxxxxxx (填写自己的密钥key)
region: ap-guangzhou (填写自己的地域名)
bucket-name: oa-bucket-1305854520 (填写自己的存储桶名称)
base-url: https://cos.hyqstudio.top/ (填写自己的访问域名)
编写接口
CosService.java
/**
* @author 追梦路上的孩子
* @version 1.0
* @date 2024/1/19 9:47
*/
public interface CosService {
/**
* 图片上传
* @param multipartFile 源文件
* @return url地址
*/
String uploadImage(MultipartFile multipartFile);
}
实现类
CosServiceImpl.java
/**
* @author 追梦路上的孩子
* @version 1.0
* @date 2024/1/19 9:48
*/
@Service
public class CosServiceImpl implements CosService {
private static final String BASE_PATH = "images/";
@Autowired
private CosConfigProperties cosConfigProperties;
@Autowired
private COSClient cosClient;
@Override
public String uploadImage(MultipartFile multipartFile) {
String originalFilename = multipartFile.getOriginalFilename();
assert originalFilename != null;
String key = getFilePath() + getFileName(originalFilename);
try {
//调用hutool工具类去判断是否为图片
String type = FileTypeUtil.getType(multipartFile.getInputStream(), true);
if (ObjectUtils.isEmpty(type) || !IMAGE_SUFFIX_LIST.contains(type)) {
throw new ServiceException(ResultStatus.IMAGE_TYPE_ERROR);
}
PutObjectRequest putObjectRequest = new PutObjectRequest(cosConfigProperties.getBucketName(),
key, multipartFile.getInputStream(), new ObjectMetadata());
cosClient.putObject(putObjectRequest);
} catch (ServiceException ex) {
throw new ServiceException(ResultStatus.IMAGE_TYPE_ERROR);
} catch (CosServiceException | IOException e) {
throw new ServiceException(ResultStatus.FILE_UPLOAD_FAILED);
}
return cosConfigProperties.getBaseUrl() + key;
}
/**
* @param oldFileName 源文件名
* @return 新的文件名
*/
private String getFileName(String oldFileName) {
return String.format("%s%s%s",
DateUtil.format(LocalDateTime.now(), "yyyyMMddHHmmss"),
RandomUtil.randomString(4),
oldFileName.substring(oldFileName.lastIndexOf(".")));
}
/**
* @return 文件存储路径
*/
private String getFilePath() {
return String.format("%s%s", BASE_PATH, DateUtil.format(LocalDateTime.now(), "yyyy-MM-dd") + "/");
}
}